@echo off
chcp 65001 >nul 2>&1
setlocal enabledelayedexpansion

echo ========================================
echo    Enterprise Logger 64-bit Builder
echo ========================================
echo.

REM 1. 检查MinGW-w64环境
echo [1/6] Checking MinGW-w64 environment...
where gcc >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo ❌ ERROR: GCC not found in PATH!
    echo.
    echo Please install MinGW-w64 and add to PATH:
    echo   - Download from: https://www.mingw-w64.org/downloads/
    echo   - Or use MSYS2: pacman -S mingw-w64-x86_64-gcc
    echo   - Add bin directory to PATH
    pause
    exit /b 1
)

REM 2. 验证编译器架构
echo [2/6] Verifying compiler architecture...
for /f "tokens=*" %%i in ('gcc -dumpmachine') do set ARCH=%%i
echo Compiler target: %ARCH%

if not "%ARCH:x86_64=%" == "%ARCH%" (
    echo ✅ 64-bit compiler detected
) else if not "%ARCH:mingw64=%" == "%ARCH%" (
    echo ✅ MinGW64 compiler detected
) else (
    echo ⚠️  WARNING: Compiler may not be 64-bit: %ARCH%
    echo Continue anyway? (Y/N)
    set /p choice=
    if /i not "!choice!"=="Y" exit /b 1
)

REM 3. 检查CMake
echo [3/6] Checking CMake...
where cmake >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo ❌ ERROR: CMake not found!
    echo Please install CMake and add to PATH
    pause
    exit /b 1
)

cmake --version | findstr /C:"cmake version"
echo ✅ CMake found

REM 4. 清理并创建构建目录
echo [4/6] Preparing build directory...
if exist build (
    echo Cleaning existing build directory...
    rmdir /s /q build
)
mkdir build
cd build

REM 5. CMake配置 (64位)
echo [5/6] Configuring CMake for 64-bit build...
echo.
echo CMake command:
echo cmake .. -G "MinGW Makefiles" ^
echo   -DCMAKE_SYSTEM_PROCESSOR=x86_64 ^
echo   -DCMAKE_SIZEOF_VOID_P=8 ^
echo   -DCMAKE_C_COMPILER=gcc ^
echo   -DCMAKE_CXX_COMPILER=g++ ^
echo   -DCMAKE_BUILD_TYPE=Release
echo.

cmake .. -G "MinGW Makefiles" ^
  -DCMAKE_SYSTEM_PROCESSOR=x86_64 ^
  -DCMAKE_SIZEOF_VOID_P=8 ^
  -DCMAKE_C_COMPILER=gcc ^
  -DCMAKE_CXX_COMPILER=g++ ^
  -DCMAKE_BUILD_TYPE=Release

if %ERRORLEVEL% NEQ 0 (
    echo ❌ CMake configuration failed!
    echo.
    echo Trying alternative configuration...
    cmake .. -G "MinGW Makefiles" -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_PROCESSOR=AMD64
    if %ERRORLEVEL% NEQ 0 (
        echo ❌ All CMake configurations failed!
        pause
        exit /b 1
    )
)

echo ✅ CMake configuration successful

REM 6. 编译
echo [6/6] Building 64-bit binaries...
echo.

REM 使用make而不是mingw32-make
make -j%NUMBER_OF_PROCESSORS%
if %ERRORLEVEL% NEQ 0 (
    echo ❌ Compilation failed!
    echo.
    echo Trying single-threaded build with verbose output...
    make VERBOSE=1
    if %ERRORLEVEL% NEQ 0 (
        echo ❌ Build failed completely!
        pause
        exit /b 1
    )
)

echo.
echo ========================================
echo           BUILD SUCCESSFUL!
echo ========================================
echo.

REM 验证生成的二进制文件
echo Verifying generated binaries...
if exist benchmark_logger.exe (
    echo ✅ benchmark_logger.exe created

    REM 尝试检查文件架构 (如果有file命令)
    file benchmark_logger.exe 2>nul && echo Architecture verified || echo Architecture check skipped
) else (
    echo ⚠️  benchmark_logger.exe not found
)

echo.
echo Generated files:
dir *.exe *.a *.dll 2>nul | findstr /v "个文件" | findstr /v "个目录"

echo.
echo Build completed! You can now run:
echo   benchmark_logger.exe
echo.
pause
